package com.haitaoit.chengquan.module.center.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.module.center.model.MyAddressModel2;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/11.
 */
public class MyAddressAdapter extends BaseAdapter {
    private Context context;
    private BackCall backCall;
    private List<MyAddressModel2> list = new ArrayList<MyAddressModel2>();
    public void setBackCall(BackCall backCall) {
        this.backCall = backCall;
    }

    public MyAddressAdapter(Context context) {
        this.context = context;
    }

    public List<MyAddressModel2> getDataList() {
        return list;
    }

    public void setList(List<MyAddressModel2> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        if (getCount() <= 0 || position >= getCount())
            return null;
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.my_address_item, null);
            holder = new ViewHolder();
            ViewUtils.inject(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.address_moren.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(addressBack!=null){
                    addressBack.methodBack(list.get(position));}
            }
        });


        holder.address_name.setText("收货人：" + list.get(position).getName());
        holder.address_phone.setText(list.get(position).getPhone());
        holder.address_add.setText("收货地址：" + list.get(position).getAddress());
        return convertView;
    }

    public interface AddressBack{
        void methodBack(MyAddressModel2 myAddressModel2);
    }

    public AddressBack addressBack;
    public void onMethodBackListener(AddressBack addressBack){
        this.addressBack=addressBack;
    }

    class ViewHolder {
        @ViewInject(R.id.address_name)
        TextView address_name;
        @ViewInject(R.id.address_phone)
        TextView address_phone;
        @ViewInject(R.id.address_add)
        TextView address_add;
        @ViewInject(R.id.address_moren)
        TextView address_moren;
    }
}
